<template>
	<view class="container">
		<view class="header gradient-bg">
			<view class="logo-wrapper">
				<text class="fab fa-weixin logo-icon"></text>
			</view>
			<text class="welcome-title">欢迎使用软件宝库</text>
			<text class="welcome-subtitle">使用微信快速登录，畅享全部功能</text>
		</view>

		<view class="main-content">
			<view class="card">
				<view class="agreement-wrapper" @click="toggleAgreement">
					<view class="checkbox-container">
						<checkbox :checked="agreed" color="#10b981" style="transform: scale(0.8);"></checkbox>
					</view>

					<view class="agreement-text-wrapper">
						<text class="agreement-text">
							我已阅读并同意
							<text class="link" @click.stop="openAgreement('user')">《用户协议》</text>
							和
							<text class="link" @click.stop="openAgreement('privacy')">《隐私政策》</text>
						</text>
					</view>
				</view>

				<view v-if="errorMessage" class="error-box">
					<text>{{ errorMessage }}</text>
				</view>

				<button :class="['login-btn', { 'btn-press-active': !isLoading && !disabledButton }]"
					:disabled="disabledButton" :loading="isLoading" @click="handleLogin">
					<text v-if="!isLoading" class="fab fa-weixin btn-icon"></text>
					<text class="btn-text">{{ isLoading ? '正在拉起微信…' : '微信一键登录' }}</text>
					<text v-if="isLoading" class="fa-solid fa-circle-notch fa-spin btn-spin"></text>
				</button>

				<text class="login-tip">
					登录仅用于身份认证，我们不会获取您的聊天记录或敏感信息
				</text>
			</view>

			<view class="guest-mode" @click="goAsGuest">
				<text class="guest-link">暂不登录，以游客身份浏览</text>
			</view>
		</view>

		<view class="footer">
			<view class="security-tip">
				<text class="fa-solid fa-shield-halved security-icon"></text>
				<text class="security-text">您的个人信息将受到严格保护</text>
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		ref,
		computed
	} from 'vue';

	// --- 状态数据 ---
	const agreed = ref(false);
	const isLoading = ref(false);
	const errorMessage = ref('');

	// 计算属性：控制按钮的禁用状态（未同意协议 或 正在加载中）
	const disabledButton = computed(() => !agreed.value || isLoading.value);

	// --- 方法 ---

	// 切换协议勾选状态
	const toggleAgreement = () => {
		agreed.value = !agreed.value;
		if (agreed.value) {
			clearError();
		}
	};

	// 显示错误信息
	const showError = (msg) => {
		errorMessage.value = msg;
	};


	// 清除错误信息
	const clearError = () => {
		errorMessage.value = '';
	};

	// 处理微信一键登录
	const handleLogin = async () => {
		if (!agreed.value) {
			showError('请先勾选并同意《用户协议》和《隐私政策》。');
			return;
		}

		clearError();
		isLoading.value = true;
		console.log('正在拉起微信授权...');

		try {
			// 模拟微信授权和登录过程
			await new Promise(resolve => setTimeout(resolve, 2000));

			// 实际项目中应在此处调用 uni.login/uni.getUserInfo 或其他微信 SDK 方法
			// 假设登录成功，并跳转
			console.log('登录成功，即将跳转');
			// uni.switchTab({ url: '/pages/index/index' }); 

		} catch (error) {
			showError(error.message || '微信登录失败，请稍后重试。');
		} finally {
			// 无论成功或失败，如果未跳转，都需要重置加载状态
			isLoading.value = false;
		}
	};

	// 以游客身份浏览
	const goAsGuest = () => {
		uni.switchTab({
			url: '/pages/index/index'
		});
	};

	const openAgreement = (type) => {
		console.log(`打开 ${type === 'user' ? '用户协议' : '隐私政策'}`);
	};
</script>

<style scoped>
	.container {
		min-height: 100vh;
		background-color: #f9fafb;
		display: flex;
		flex-direction: column;
	}

	/* --- 顶部装饰 --- */
	.header {
		padding: 80rpx 40rpx 60rpx;
		text-align: center;
		color: #ffffff;
	}

	.gradient-bg {
		background-image: linear-gradient(135deg, #22c55e 0%, #16a34a 100%);
	}

	.logo-wrapper {
		width: 160rpx;
		height: 160rpx;
		background-color: rgba(255, 255, 255, 0.2);

		border-radius: 40rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		margin: 0 auto 32rpx;
	}

	.logo-icon {
		font-size: 64rpx;
		color: #ffffff;
	}

	.welcome-title {
		font-size: 40rpx;
		font-weight: bold;
		display: block;
	}

	.welcome-subtitle {
		font-size: 28rpx;
		opacity: 0.9;
		margin-top: 8rpx;
		display: block;
	}

	/* --- 主体内容 --- */
	.main-content {
		flex: 1;
		padding: 0 48rpx;
		margin-top: -64rpx;
	}

	.card {
		background-color: #ffffff;
		border-radius: 32rpx;
		box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.1), 0 1px 2px -1px rgba(0, 0, 0, 0.1);
		padding: 48rpx;
	}

	/* 协议勾选 */
	.agreement-wrapper {
		display: flex;
		flex-direction: row;
		align-items: flex-start;
		gap: 24rpx;
	}

	.checkbox-container {
		display: flex;
		align-items: center;
		justify-content: center;
		width: 40rpx;
		height: 40rpx;
		margin-top: 8rpx;
		flex-shrink: 0;
	}

	.agreement-text-wrapper {
		flex: 1;
	}

	.agreement-text {
		font-size: 28rpx;
		color: #4b5563;
		line-height: 40rpx;
	}

	.link {
		color: #10b981;
		text-decoration: underline;
	}

	/* 错误提示 */
	.error-box {
		margin-top: 24rpx;
		font-size: 28rpx;
		color: #dc2626;
		background-color: #fef2f2;
		border: 1rpx solid #fecaca;
		border-radius: 16rpx;
		padding: 24rpx;
		text-align: center;
		display: block;
	}

	/* 登录按钮 */
	.login-btn {
		margin-top: 40rpx;
		width: 100%;
		/* background-color: #22c55e; */
		color: #ffffff;
		padding: 28rpx 0;
		/* py-4 */
		border-radius: 24rpx;
		/* rounded-xl */
		font-weight: bold;
		font-size: 32rpx;
		/* text-base */
		box-shadow: 0 10rpx 15rpx -3rpx rgba(0, 0, 0, 0.1), 0 4rpx 6rpx -4rpx rgba(0, 0, 0, 0.05);

		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: center;
		gap: 16rpx;
		line-height: 1;
	}

	.login-btn::after {
		border: none;
	}


	/* 按钮按下效果 (UniApp button的:active或自定义类) */
	.login-btn:active,
	.btn-press-active:active {
		transform: translateY(2rpx);
		/* btn-press */
		box-shadow: none;
	}

	/* 禁用状态 */
	/* 	.login-btn[disabled] {
		opacity: 0.7;

		cursor: not-allowed;
		transform: translateY(0);
	} */

	.btn-icon {
		font-size: 36rpx;
	}

	.btn-spin {
		font-size: 32rpx;
		margin-left: 10rpx;
	}

	.login-tip {
		margin-top: 24rpx;
		font-size: 24rpx;
		color: #9ca3af;
		text-align: center;
		display: block;
	}

	/* 游客模式 */
	.guest-mode {
		text-align: center;
		margin-top: 48rpx;
	}

	.guest-link {
		color: #6b7280;
		font-size: 28rpx;
	}

	/* --- 底部安全提示 --- */
	.footer {
		padding: 0 48rpx 64rpx;
	}

	.security-tip {
		background-color: #ecfdf5;
		border-radius: 24rpx;
		padding: 32rpx;
		text-align: center;
		display: flex;
		flex-direction: column;
		align-items: center;
	}

	.security-icon {
		color: #059669;
		font-size: 40rpx;
		margin-bottom: 16rpx;
	}

	.security-text {
		font-size: 28rpx;
		color: #047857;

	}
</style>